import Mock from 'mockjs';

const userData = [{
    username: 'aaa',
    password: '123'
  },
  {
    username: 'bbb',
    password: '456'
  }
];

const data = Mock.mock({
  'list|50': [{
    id: "@id",
    name: "@ctitle",
    "type|1": ["鸡翅", "汉堡", "可乐", "套餐"],
    image: "@image(100X100,@color,@city)",
    num: 0,
    "stock|0-100": 1
  }]
});

console.log('data', data);

const obj = {};

data.list.forEach((item) => {
  if (obj[item.type]) {
    obj[item.type].push(item);
  } else {
    obj[item.type] = [];
    obj[item.type].push(item);

  }
})

console.log('obj', obj);

Mock.mock('/api/list', obj)

Mock.mock('/api/login', 'post', function (option) {
  let {
    username,
    password
  } = JSON.parse(option.body);
  console.log('option.body', option.body);
  let flag = userData.some((item) => {

    return username + '' === item.username + '' && password + '' === item.password + '';
  });


  if (flag) {
    return {
      success: 1,
      message: '用户登录成功',
      token: new Date().getTime() + username
    }
  } else {
    return {
      success: 0,
      message: '用户名或者密码错误'
    }
  }
});